# Name:    CExceptionHnd.py
# Date:    Sun May  2 15:18:25 CEST 2010
# Purpose: Handles exceptions
# Def		  CExceptionHnd(extExcHnd_cb)
# Inputs:  

import sys

####################################################################################################

class CExceptionHnd():
	'''
	'' Base class for handling exceptions
	'''
	__excType = None
	__excVal = None
	__msg = None
	__doExit = None
	# callable object which should reference to external function 'handleException' in derived class
	__extExcHnd_cb = None


#---------------------------------------------------------------------------------------------------

	def __init__(self):
		#self.__errno = 0
		self.__doExit = False

#---------------------------------------------------------------------------------------------------

#	def hasException(self):
#		if self.__errno == 0:
#			return False
#		else:
#			return True 

#---------------------------------------------------------------------------------------------------

	#def handle(self, e = None):
	def handle(self):
		''' handles known exceptions '''

		self.__excType, self.__excVal = sys.exc_info()[:2]

		#TODO: to be moved to the log file
		print 'CExceptionHnd:',self.__class__,self.__excType,self.__excVal

		# handling general exceptions
		if self.__excType is IOError:
			if self.__excVal.filename is not None:
				self.__msg = self.__excVal.strerror + ": '" + self.__excVal.filename + "'"
			else:
				self.__msg = self.__excVal
			self.__doExit = True
		
		self.handleException()

		if self.__extExcHnd_cb is not None:
			self.__extExcHnd_cb(self)
		else:
			print 'No external exception handler defined.'

#---------------------------------------------------------------------------------------------------

	def cleanExc(self):
		self.__excType = None
		self.__excVal = None
		self.__msg = None

#---------------------------------------------------------------------------------------------------

	# override method in the derived class
	def handleException(self):
		pass

#---------------------------------------------------------------------------------------------------

	def getExcType(self):
		return self.__excType

#---------------------------------------------------------------------------------------------------

	def setExcType(self, val):
		self.__excType = val

#---------------------------------------------------------------------------------------------------

	def getExcVal(self):
		return self.__excVal

#---------------------------------------------------------------------------------------------------

	def setExcVal(self, val):
		self.__excVal = val

#---------------------------------------------------------------------------------------------------

	def getMsg(self):
		return str(self.__msg)

#---------------------------------------------------------------------------------------------------

	def setMsg(self, val):
		self.__msg = val

#---------------------------------------------------------------------------------------------------

	def getDoExit(self):
		return self.__doExit

#---------------------------------------------------------------------------------------------------

	def setDoExit(self, val):
		self.__doExit = val

#---------------------------------------------------------------------------------------------------

	def getExterExcHndCB(self):
		return self.__extExcHnd_cb

#---------------------------------------------------------------------------------------------------

	def setExterExcHndCB(self, fnc):
		if callable(fnc):
			self.__extExcHnd_cb = fnc

####################################################################################################
